package xyz.zq.cloud.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.zq.cloud.admin.service.AccountService;
import xyz.zq.cloud.admin.service.CompanyService;
import xyz.zq.cloud.bs.api.dto.AccountDTO;
import xyz.zq.cloud.bs.api.dto.UserInfo;
import xyz.zq.cloud.bs.api.entity.Account;
import xyz.zq.cloud.bs.api.entity.Company;
import xyz.zq.cloud.common.core.constant.CommonConstants;
import xyz.zq.cloud.common.core.util.AccountUtil;
import xyz.zq.cloud.common.core.util.R;
import xyz.zq.cloud.common.security.annotation.Inner;
import xyz.zq.cloud.common.security.service.CloudUser;
import xyz.zq.cloud.common.security.util.SecurityUtils;

/**
 * <p>
 * 租户商户用户 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2020-05-11
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class AccountController {


    private final AccountService accountService;
    private final CompanyService companyService;

    /**
     * 获取当前用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping("/info")
    public R<UserInfo> info() {
        CloudUser cloudUser = SecurityUtils.getUser();
        Assert.notNull(cloudUser, "cloudUser must be not null");
        Account user = accountService.getOne(Wrappers.<Account>query()
                .lambda().eq(Account::getUsername, cloudUser.getUsername())
                .eq(Account::getCompanyId, cloudUser.getCompanyId()));
        if (ObjectUtil.isEmpty(user))
            return R.failed("获取当前用户信息失败");
        return R.ok(accountService.getUserInfo(user));
    }

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    @Inner
    @GetMapping("/info/{username}")
    public R<UserInfo> info(@PathVariable String username) {

        R<String[]> rs = AccountUtil.checkUserName(username);
        if (rs.getCode() == CommonConstants.FAIL)
            return R.failed(rs.getMsg());

        R<Company> rc = companyService.getCompanyBySuffix(rs.getData()[1]);
        if (rc.getCode() == CommonConstants.FAIL)
            return R.failed(rs.getMsg());

        Account user = accountService.getOne(Wrappers.<Account>query()
                .lambda().eq(Account::getUsername, rs.getData()[0])
                .eq(Account::getCompanyId, rc.getData().getId()));
        if (ObjectUtil.isEmpty(user))
            return R.failed(String.format("用户信息为空 %s", username));

        return R.ok(accountService.getUserInfo(user));
    }

    /**
     * 分页查询用户
     *
     * @param page       参数集
     * @param accountDTO 查询参数列表
     * @return 用户集合
     */
    @GetMapping("/page")
    public R getUserPage(Page page, AccountDTO accountDTO) {
        return R.ok(accountService.getUserWithRolePage(page, accountDTO));
    }


}

